﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.Core;
using System.Collections.Generic;
using System.Xml;
using System.Reflection;

namespace Jungle.Admin.Controls
{
	public partial class NodeEdit : System.Web.UI.UserControl
	{
		int nodeID;
		string cmd;
        private string LangAlias = "ru/";

		protected void Page_Load(object sender, EventArgs e) {
            GetLanhAlias();
			if (Request["nodeID"] != null)
				nodeID = int.Parse(Request["nodeID"]);
			if (Request["cmd"] != null)
				cmd = Request["cmd"];

			if (!this.IsPostBack) {
				switch (cmd) {
					case "edit":
						BindDataForEdit(nodeID);
						break;
					case "add":
						BindDataForAdd(nodeID);
						break;

				}
			}
		}
		protected void BindDataForEdit(int nodeID) {
			Node node = new Node();
			node = Node.GetByNodeID(nodeID);
			BindDefaultData(node);

			tbTitle.Text = node.Title;
			tbAlias.Text = node.Alias;
            tbName.Text = node.Name;
			tbKeywords.Text = node.Keywords;
			tbDescription.Text = node.Description;
			if (node.ParentNodeID != 0)
				ddlParentNodeID.Items.FindByValue(node.ParentNodeID.ToString()).Selected = true;
			ddlTemplateID.Items.FindByValue(node.TemplateID.ToString()).Selected = true;
			cbEnabled.Checked = node.Enabled;
		}
		protected void BindDataForAdd(int parentNodeID) {
			Node parentNode = new Node();
			parentNode = Node.GetByNodeID(parentNodeID);
            tbTitle.Text = parentNode.Title;
			BindDefaultData(parentNode);
			ddlParentNodeID.Items.FindByValue(parentNodeID.ToString()).Selected = true;
		}
		protected void BindDefaultData(Node node) {
			//parentNodeID
			Node topNode = new Node();
			topNode = Node.GetTopByLangID(node.LangID);
			ddlParentNodeID.Items.Add(new ListItem(topNode.Name, topNode.NodeID.ToString()));
			BuildTree(topNode.NodeID, 1);
			//templateID
			ddlTemplateID.DataSource = Template.GetAll();
			ddlTemplateID.DataBind();
		}
		private void BuildTree(int parentNodeID, int level) {
			List<Node> listNodes = new List<Node>();
			listNodes = Node.GetByParentNodeID(parentNodeID);
			foreach (Node n in listNodes) {
				ddlParentNodeID.Items.Add(new ListItem(GetLenghtStr(level*2) + n.Name  , n.NodeID.ToString()));				
                BuildTree(n.NodeID, level + 1);
			}

		}
		private string GetLenghtStr(int len) {
			string str = "";
			for (int i = 0; i < len; i++)
				str += ".";
			return str;
		}

        protected void GetLanhAlias()
        {
            HttpCookie cookie = Request.Cookies["CurrLang"];
            if (cookie != null)
            {
                Language lang = new Language(Convert.ToInt32(cookie["lang"]), "", "", true, 0);
                lang.GetById();

                LangAlias = lang.Name + "/";
            }
        }
        
		protected void btnSave_Click(object sender, EventArgs e) {
			if (cmd == "edit") {
				Node node = new Node();
                Node thisNode = new Node();
                Node parentNode = new Node();
                thisNode = Node.GetByNodeID(nodeID);
                parentNode = Node.GetByNodeID(thisNode.ParentNodeID);
				node = Node.GetByNodeID(nodeID);
                node.Name = tbName.Text;
				node.Title = tbTitle.Text;
                if (thisNode.ParentNodeID != 0) node.Alias = tbAlias.Text;
				node.Keywords = tbKeywords.Text;
				node.Description = tbDescription.Text;
                if (thisNode.ParentNodeID != 0) node.ParentNodeID = int.Parse(ddlParentNodeID.SelectedValue);
				node.TemplateID = int.Parse(ddlTemplateID.SelectedValue);
                if (thisNode.ParentNodeID != 0) node.RelativeUrl = LangAlias + node.Alias;
                node.Enabled = cbEnabled.Checked;
				Node.Update(node);
				/***/
				Response.Redirect(Request.Url.ToString());
			}
			if (cmd == "add") {
				Node parentNode = new Node();
				parentNode = Node.GetByNodeID(nodeID);
				Node node = new Node();
                node.Name = tbName.Text;
				node.Title = tbTitle.Text;
                node.Alias = tbAlias.Text; //InsertUrlMap(parentNode.Alias + "/"+ tbAlias.Text);
				node.Keywords = tbKeywords.Text;
				node.Description = tbDescription.Text;
				node.ParentNodeID = int.Parse(ddlParentNodeID.SelectedValue);
				node.TemplateID = int.Parse(ddlTemplateID.SelectedValue);
				node.Enabled = cbEnabled.Checked;
				node.LangID = parentNode.LangID;
				node.RelativeUrl = LangAlias + node.Alias;
				Node.Insert(node);
				Response.Redirect(Request.Url.ToString());
			}
		}

        protected void InsertUrlMap(string url)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/Web.config"));

            XmlNode node = doc["configuration"]["system.web"]["urlMappings"];

            XmlElement elem = doc.CreateElement("add");
            elem.SetAttribute("url", "~/" + url);
            elem.SetAttribute("mappedUrl", "~/default.aspx");

            node.InsertAfter(elem, node.FirstChild);

            doc.Save(Server.MapPath("~/Web.config"));        
        }

	}
}